package cn.tedu.luban.order.main.producer;

import cn.tedu.luban.order.main.service.MessageService;
import cn.tedu.luban.order.protocol.dto.OrderMessageDTO;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class MessageServiceImpl implements MessageService {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override public void sendDelayMessage(String orderNo) {
        Message<String> message = MessageBuilder.withPayload(orderNo).build();
        rocketMQTemplate.asyncSend("order_delay_topic", message, new SendCallback() {
            @Override public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }

            @Override public void onException(Throwable throwable) {
                System.out.println("发送失败");
            }
        }, 1000, 4);
    }

    @Override public void sendFinishMessage(OrderMessageDTO dto) {
        Message<OrderMessageDTO> message = MessageBuilder.withPayload(dto).build();
        rocketMQTemplate.asyncSend("order_finish_topic", message, new SendCallback() {
            @Override public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }

            @Override public void onException(Throwable throwable) {
                System.out.println("发送失败");
            }
        });
    }
}
